package com.qmx.launcher.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.qmx.launcher.QMXApplication;
import com.qmx.launcher.activity.MiracastActivity;
import com.qmx.launcher.bean.UsbData;

import java.io.File;

/**
 * Created by demo on 2017/4/1 0001
 */
public class UsbReceiver extends BroadcastReceiver {

    public DataCallBack mDataCallBack;

    public UsbReceiver(DataCallBack dataCallBack) {
        mDataCallBack = dataCallBack;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
                //USB设备移除，更新UI
                Log.d("UsbReceiver", "SB设备移除，更新UI >>>>> ");
                //                Toast.makeText(context, "USB设备移除", Toast.LENGTH_SHORT).show();
                //                System.exit(0);
            } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
                //USB设备挂载，更新UI
                String usbPath = intent.getData().getPath() + "/QMX";
                //（usb在手机上的路径）
                File storage = new File(usbPath);
                File[] files = storage.listFiles();
                Log.i("usbPath", usbPath);
                Log.i("onReceivefiles", files.length + "");
                for (final File file : files) {
                    if (file.canRead() && file.getName().endsWith("txt")) {
                        Log.i("filesname", file.getName() + "");
                        if (mDataCallBack != null) {
                            if (usbPath != null && file.getName() != null) {
                                mDataCallBack.onDataChanged(usbPath, file.getName());
                                Log.i("mDataCallBack", file.getName());
                            }

                        }
                    }
                }
            }
        }
    }

    public interface DataCallBack {
        void onDataChanged(String path, String fileName);
    }
}
